// pages/shopCar/shopCar.js
import Dialog from '/@vant/weapp/dialog/dialog';

import {
  HTTP
} from '../../utils/http-p.js'
const http = new HTTP;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    shopCartList: [],
    sumPrice: 0
  },

  onChange(event) {
    const check = event.detail
    const list = this.data.shopCartList
    const index = event.currentTarget.dataset.index
    list[index].checked = check
    this.setData({
      shopCartList: list,
    });
    const flag = this.checkRadio()
    this.setData({
      checked: flag
    })
    this.priceSum()
  },

  /**
   * 小选中监听大选中
   */
  checkRadio() {
    let flag = true
    let shopCartList = this.data.shopCartList
    for (let i = 0; i < shopCartList.length; i++) {
      if (!shopCartList[i].checked) {
        flag = false
      }
    }
    return flag
  },

  onChangeAll(event) {
    let check = event.detail
    const list = this.data.shopCartList
    for (var i = 0; i < list.length; i++) {
      list[i].checked = check
    }
    this.setData({
      shopCartList: list,
      checked: check
    });
    this.priceSum()
  },

  /**
   * 计算价格
   * @param {id} event 
   */
  priceSum() {
    let list = this.data.shopCartList
    let sum = 0
    for (let i = 0; i < list.length; i++) {
      if (list[i].checked) {
        sum += list[i].productPrice * list[i].productQuantity
      }
    }
    let sumPrice = sum * 100
    this.setData({
      sumPrice: sumPrice
    })
  },

  /**
   * 监听数据量
   */
  onChangeQuantity(event) {
    const id = event.currentTarget.dataset.id
    const value = event.detail
    const index = event.currentTarget.dataset.index
    let list = this.data.shopCartList
    list[index].productQuantity = value
    if (value !== '') {
      this.updateQuantity(id, value)
      this.priceSum()
      this.setData({
        shopCartList: list
      })
    }
  },

  /**
   * 查询购物车
   */
  getShopCartList() {
    let openId = wx.getStorageSync('openId');
    http.request({
      url: `product-cart/${openId}`,
      success: (res) => {
        const list = res.data;
        for (var i = 0; i < list.length; i++) {
          list[i].checked = false
        }
        this.setData({
          shopCartList: list
        })

      }
    })
  },

  /**
   * 删除购物车点击事件
   */
  deleteTab(event) {
    const id = event.currentTarget.dataset.id
    Dialog.confirm({
      title: '提示',
      message: '将此产品移除购物车？'
    }).then(() => {
      this.removeShopCartById(id);
    }).catch(() => {
      // on cancel
    });
  },

  /**
   * 删除购物车
   * @param {id} options 
   */
  removeShopCartById(id) {
    http.request({
      url: 'product-cart/' + id,
      method: 'delete',
      success: (res) => {
        this.getShopCartList()
      }
    })
  },

  /**
   * 更新购物车数量
   * @param {id} options 
   */
  updateQuantity(id, value) {
    http.request({
      url: `product-cart/${id}/${value}`,
      method: 'put',
      success: (res) => {
        this.priceSum()
      }
    })
  },
  /**
   * 提交订单
   */
  onClickButton() {
    let list = this.data.shopCartList
    let submitList = []
    for (let i = 0; i < list.length; i++) {
      if (list[i].checked) {
        submitList.push(list[i]);
      }
    }
    wx.navigateTo({
      url: "/pages/orderAccount/orderAccount?orderList=" + JSON.stringify(submitList) + "&sumPrice=" + this.data.sumPrice
    });
  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getShopCartList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})